package com.pro.controller;

import com.pro.pojo.*;
import com.pro.service.MicrocircleService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 谭金诚
 * @Description TODO
 * @Date 2022/1/10 10:58
 * @Version 1.0
 */
@Controller
@RequestMapping("/microcircle")
public class MicrocircleController {

    @Autowired
    private MicrocircleService service;

    @Autowired
    private FastDFSClientUtil fsc;

    /**
     * @ClassName getLog
     * @Author 谭金诚
     * @Date 2022/1/10
     */
    @RequestMapping("/getLog")
    @ResponseBody
    public JsonData getLog(JsLog jsLog){
        List<JsLog> jsLogs = service.getLog(jsLog);
        return jsLogs != null ? JsonData.buildSuccess(jsLogs) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName delLog
     * @Author 谭金诚
     * @Date 2022/1/10
     */
    @RequestMapping("/delLog")
    @ResponseBody
    public JsonData delLog(Integer logId){
        boolean flag = service.delLog(logId);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getMyMicrocircleList
     * @Author 谭金诚
     * @Date 2022/1/10
     */
    @RequestMapping("/getMyMicrocircleList")
    @ResponseBody
    public JsonData getMyMicrocircleList(JsMicrocircle jsMicrocircle){
        List<JsMicrocircle> myMicrocircleList = service.getMyMicrocircleList(jsMicrocircle);
        return myMicrocircleList != null ? JsonData.buildSuccess(myMicrocircleList) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getMyMicrocircleCollectionNums
     * @Author 谭金诚
     * @Date 2022/1/10
     */
    @RequestMapping("/getMyMicrocircleCollectionNums")
    @ResponseBody
    public JsonData getMyMicrocircleCollectionNums(Integer microcircleId){
        Integer myMicrocircleCollectionNums = service.getMyMicrocircleCollectionNums(microcircleId);
        return myMicrocircleCollectionNums != 0 ? JsonData.buildSuccess(myMicrocircleCollectionNums) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName delMicroCircle
     * @Author 谭金诚
     * @Date 2022/1/10
     */
    @RequestMapping("/delMicroCircle")
    @ResponseBody
    public JsonData delMicroCircle(Integer microcircleId){
        boolean flag = service.delMicroCircle(microcircleId);
        return  flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName queryMicroCircleClass
     * @Author 谭金诚
     * @Date 2022/1/11
     */
    @RequestMapping("/queryMicroCircleClass")
    @ResponseBody
    public JsonData queryMicroCircleClass(Integer acticleCategroyId){
        JsActicleCategroy jsActicleCategroy = service.queryMicroCircleClass(acticleCategroyId);
        return jsActicleCategroy != null ? JsonData.buildSuccess(jsActicleCategroy) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getMicroCircleComment
     * @Author 谭金诚
     * @Date 2022/1/11
     */
    @RequestMapping("/getMicroCircleComment")
    @ResponseBody
    public JsonData getMicroCircleComment(Integer commodityId, Integer type){
        List<JsComment> microCircleComment = service.getMicroCircleComment(commodityId, type);
        return microCircleComment != null ? JsonData.buildSuccess(microCircleComment) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName hideComment
     * @Author 谭金诚
     * @Date 2022/1/11
     */
    @RequestMapping("/hideComment")
    @ResponseBody
    public JsonData hideComment(Integer id, Integer display){
        boolean flag = service.hideComment(id, display);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName delComment
     * @Author 谭金诚
     * @Date 2022/1/11
     */
    @RequestMapping("/delComment")
    @ResponseBody
    public JsonData delComment(Integer id, String  status){
        boolean flag = service.delComment(id, status);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addMicroCircleComment
     * @Author 谭金诚
     * @Date 2022/1/12
     */
    @RequestMapping("/addMicroCircleComment")
    @ResponseBody
    public JsonData addMicroCircleComment(JsComment jsComment){
        boolean flag = service.addMicroCircleComment(jsComment);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getJsActicleCategroy
     * @Author 谭金诚
     * @Date 2022/1/12
     */
    @RequestMapping("/getJsActicleCategroy")
    @ResponseBody
    public JsonData getJsActicleCategroy(){
        List<JsActicleCategroy> jsActicleCategroy = service.getJsActicleCategroy();
        return jsActicleCategroy != null ? JsonData.buildSuccess(jsActicleCategroy) : JsonData.buildError("服务器错误");
    }

    /**
     * @ClassName addZtree
     * @Author 谭金诚
     * @Date 2022/1/13
     */
    @RequestMapping("/addZtree")
    @ResponseBody
    public JsonData addZtree(JsActicleCategroy jsActicleCategroy){
        boolean flag = service.addZtree(jsActicleCategroy);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName delZtree
     * @Author 谭金诚
     * @Date 2022/1/13
     */
    @RequestMapping("/delZtree")
    @ResponseBody
    public JsonData delZtree(Integer acticleCategroyId){
        boolean flag = service.delZtree(acticleCategroyId);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName updateZtree
     * @Author 谭金诚
     * @Date 2022/1/13
     */
    @RequestMapping("/updateZtree")
    @ResponseBody
    public JsonData updateZtree(String artcleCategroyName, Integer acticleCategroyId) {
        boolean flag = service.updateZtree(artcleCategroyName, acticleCategroyId);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName getDraftMicrocircle
     * @Author 谭金诚
     * @Date 2022/1/14
     */
    @RequestMapping("/getDraftMicrocircle")
    @ResponseBody
    public JsonData getDraftMicrocircle(Integer details) {
        JsMicrocircle draftMicrocircle = service.getDraftMicrocircle(details);
        return draftMicrocircle != null ? JsonData.buildSuccess(draftMicrocircle) : JsonData.buildError("服务器错误");
    }

    /**
     * @ClassName upload
     * @Author 谭金诚
     * @Date 2022/1/14
     * @Return map集合
     * 微圈上传图片
     */
    @RequestMapping("/upload")
    @ResponseBody
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile[] file, HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String,Object>();
        String resAccessUrl = null;
        for (MultipartFile multipartFile : file) {
            try {
                //数据库文件路径
                String fileUrl = fsc.uploadFile(multipartFile);
                //前台传递路径
                request.setAttribute("msg", "成功上传文件，  '" + fileUrl + "'");
                resAccessUrl = fsc.getResAccessUrl(fileUrl);
                int i = resAccessUrl.lastIndexOf("/");
                String split = resAccessUrl.substring(i+1);
                map.put("src",resAccessUrl);
                map.put("title",split);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map != null ? JsonData.buildSuccess(map):JsonData.buildError("服务器错误");
    }

    /**
     * @ClassName updateDraftMicrocircle
     * @Author 谭金诚
     * @Date 2022/1/14
     * @Return boolean
     * 修改草稿微圈信息
     */
    @RequestMapping("/updateDraftMicrocircle")
    @ResponseBody
    public JsonData updateDraftMicrocircle(JsMicrocircle jsMicrocircle){
        boolean flag = service.updateDraftMicrocircle(jsMicrocircle);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addDraftMirList
     * @Author 谭金诚
     * @Date 2022/1/14
     * @Return boolean
     * 添加微圈
     */
    @RequestMapping("/addDraftMirList")
    @ResponseBody
    public JsonData addDraftMirList(JsMicrocircle jsMicrocircle){
        jsMicrocircle.setArtcleKeywords("微圈");
        jsMicrocircle.setCreateDate(new Date());
        jsMicrocircle.setUpdateDate(jsMicrocircle.getCreateDate());
        jsMicrocircle.setReleaseDate(null);
        jsMicrocircle.setActicleHits(0);
        jsMicrocircle.setActicleLike(0);
        jsMicrocircle.setActicleReviews(0);
        jsMicrocircle.setActicleShare(0);
        jsMicrocircle.setShow(2);
        jsMicrocircle.setSerialNumber(0);
        boolean flag = service.addDraftMirList(jsMicrocircle);
        System.err.println(jsMicrocircle);
        return flag != false ? JsonData.buildSuccess(jsMicrocircle) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addMirImg
     * @Author 谭金诚
     * @Date 2022/1/14
     * @Return JsMicrocircleImg
     * 发布微圈（发布草稿微圈）
     */
    @RequestMapping("/addMirImg")
    @ResponseBody
    public JsonData addMirImg(JsMicrocircleImg jsMicrocircleImg){
        boolean flag = service.addMirImg(jsMicrocircleImg);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addMicrocircle
     * @Author 谭金诚
     * @Date 2022/1/14
     * @Return JsMicrocircle
     * 发布微圈（直接发布）
     */
    @RequestMapping("/addMicrocircle")
    @ResponseBody
    public JsonData addMicrocircle(JsMicrocircle jsMicrocircle){
        jsMicrocircle.setArtcleKeywords("微圈");
        jsMicrocircle.setCreateDate(new Date());
        jsMicrocircle.setUpdateDate(jsMicrocircle.getCreateDate());
        jsMicrocircle.setReleaseDate(jsMicrocircle.getCreateDate());
        jsMicrocircle.setActicleHits(0);
        jsMicrocircle.setActicleLike(0);
        jsMicrocircle.setActicleReviews(0);
        jsMicrocircle.setActicleShare(0);
        jsMicrocircle.setShow(2);
        jsMicrocircle.setSerialNumber(0);
        boolean flag = service.addMicrocircle(jsMicrocircle);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    /**
     * @ClassName addLog
     * @Author 谭金诚
     * @Date 2022/1/17
     * @Return boolean
     * 添加微圈操作日志
     */
    @RequestMapping("/addLog")
    @ResponseBody
    public JsonData addLog(JsLog jsLog){
        boolean flag = service.addLog(jsLog);
        return flag != false ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }


    /*
     * @author Ypq
     * @date 2022/2/08 10:11
     * @return com.pro.pojo.JsMicrocircle
     */
    @RequestMapping("/getStatistics")
    @ResponseBody
    public JsonData getStatistics(Integer acticleCategroyId){
        List<JsMicrocircle> hitsLikeNums = service.getStatistics();
        return hitsLikeNums != null ? JsonData.buildSuccess(hitsLikeNums) : JsonData.buildError("服务端错误");
    }

}
